package demo1;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86138
 * Date: 2024-07-23
 * Time: 19:53
 */
public class CardDemo {
    //买52张牌
    public static final String[] suits = {"♦","♣","♥","♠"};

    public List<Card> buy(){
        List<Card> cardList = new ArrayList<>();
        for (int i = 1; i <= 13; i++) {
            for (int j = 0; j <= 3; j++) {
                Card card = new Card(suits[j],i);
                cardList.add(card);
            }
        }

        return cardList;
    }

    //洗牌
    public void ranCard(List<Card> cardList){
        Random random = new Random();
        for (int i = cardList.size()-1; i > 0; i--) {
            int index = random.nextInt(i);//1~i
            swap(index,i,cardList);
        }
    }

    private void swap(int index,int i,List<Card>cardList){
        //arr[index] = t;
        //arr[index] = arr[i];
        //arr[i] = t;
        Card t = cardList.get(index);
        cardList.set(index,cardList.get(i));
        cardList.set(i,t);
    }

    //分牌
    public List<List<Card>> fenCard(List<Card> cardList){
        List<Card> hand0 = new ArrayList<>();
        List<Card> hand1 = new ArrayList<>();
        List<Card> hand2 = new ArrayList<>();

        List<List<Card>> hand = new ArrayList<>();
        hand.add(hand0);
        hand.add(hand1);
        hand.add(hand2);

        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                Card card = cardList.remove(0);
                hand.get(j).add(card);
            }
        }
        return hand;
    }
}
